package com.cloudree.gllib.shape;

import android.opengl.GLES20;

import com.cloudree.gllib.BaseApp;
import com.cloudree.gllib.R;
import com.cloudree.gllib.constants.RenderData;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

/**
 * Created by Administrator on 2017/8/29.
 */
public class HockeyTable extends Shape {
    public static final int PER_VERTEX_SIZE = 4;
    public static final int PER_COLOR_SIZE = 4;
    public static final int BYTES_PER_FLOAT = 4;

    @Override
    public void loadingMaterial() {
        float[] vertexData = RenderData.Hockey.ver4Data;
        vertexBuffer = ByteBuffer.allocateDirect(vertexData.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer().put(vertexData);
        vertexBuffer.position(0);
    }

    @Override
    public void compileShader() {
        int vertexShader = generateShader(GLES20.GL_VERTEX_SHADER, getVertexShaderCode());
        int fragmentShader = generateShader(GLES20.GL_FRAGMENT_SHADER, getFragmentShaderCode());

        String[] attr = new String[]{"aPosition", "aColor"};
        mProgram = generateGlProgram(vertexShader, fragmentShader, attr);
    }

    @Override
    public void draw(float[] mvpMatrix, float[] modelMatrix, float[] viewMatrix, float[] projectionMatrix) {
        GLES20.glUseProgram(mProgram);
        int matrixHandle = GLES20.glGetUniformLocation(mProgram, "uMatrix");
        int colorHandle = GLES20.glGetAttribLocation(mProgram, "aColor");
        int positionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");

        //allocate position
        vertexBuffer.position(0);//reset position
        GLES20.glVertexAttribPointer(positionHandle, PER_VERTEX_SIZE, GLES20.GL_FLOAT, false, (PER_VERTEX_SIZE + PER_COLOR_SIZE) * BYTES_PER_FLOAT, vertexBuffer);
        GLES20.glEnableVertexAttribArray(positionHandle);

        vertexBuffer.position(PER_VERTEX_SIZE);//allocate color
        GLES20.glVertexAttribPointer(colorHandle, PER_COLOR_SIZE, GLES20.GL_FLOAT, false, (PER_VERTEX_SIZE + PER_COLOR_SIZE) * BYTES_PER_FLOAT, vertexBuffer);
        GLES20.glEnableVertexAttribArray(colorHandle);

        //draw shape
//        GLES20.glUniform4f(colorHandle, 1.0f, 1.0f, 1.0f, 1.0f);//allocate uniform color
        GLES20.glUniformMatrix4fv(matrixHandle, 1, false, projectionMatrix, 0);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);

//        draw line
//        GLES20.glUniform4f(colorHandle, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2);

        //draw points
//        GLES20.glUniform4f(colorHandle, 0.0f, 1.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1);

//        GLES20.glUniform4f(colorHandle, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_POINTS, 9, 1);
    }

    @Override
    public String getVertexShaderCode() {
        return readRawFile(BaseApp.getApp(), R.raw.hockey_vertex_shader);
    }

    @Override
    public String getFragmentShaderCode() {
        return readRawFile(BaseApp.getApp(), R.raw.hockey_fragment_shader);
    }
}
